package com.jsmile.cloud.authc.sys.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.authc.api.req.sys.ReqSysResourcePageQuery;
import com.jsmile.cloud.authc.api.req.sys.ReqSysResourceQuery;
import com.jsmile.cloud.authc.api.req.sys.ReqSysResourceUpdate;
import com.jsmile.cloud.authc.api.res.ResSysResources;
import com.jsmile.cloud.authc.sys.SysResourcesApplicationService;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.LogTypeEnum;
import com.jsmile.mall.api.to.JsmilePage;
import com.jsmile.mall.log.annotation.JsmileLog;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 控制器
 * @date 2018年08月25日 04:57:24
 */
@RestController
@Api(tags = "资源管理")
@Slf4j
@AllArgsConstructor
@RequestMapping("/sys/menu")
public class SysResourcesController extends AuthcController {

    private final SysResourcesApplicationService sysResourcesApplicationService;

    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    public JSmileResult<JsmilePage<ResSysResources>> getPageList(@RequestBody @Valid ReqSysResourcePageQuery reqSysResourcePageQuery, @CurrentContext JsmileContext jsmileContext) {
        reqSysResourcePageQuery.setCurTenantId(jsmileContext.getTenantId());
        IPage<ResSysResources> pages = sysResourcesApplicationService.getPageList(reqSysResourcePageQuery);
        return JSmileResult.success(new JsmilePage(reqSysResourcePageQuery.getPageIndex(), reqSysResourcePageQuery.getPageSize(), pages.getTotal(), pages.getRecords()));
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    public JSmileResult<List<ResSysResources>> getList(@RequestBody @Valid ReqSysResourceQuery reqSysResourceQuery, @CurrentContext JsmileContext jsmileContext) {
        reqSysResourceQuery.setCurTenantId(jsmileContext.getTenantId());
        List<ResSysResources> list = sysResourcesApplicationService.getList(reqSysResourceQuery);
        return JSmileResult.success(list);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "新增资源")
    @JsmileLog(title = "用户中心->新增资源", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult saveDict(@RequestBody @Valid ReqSysResourceUpdate reqSysResourceUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysResourceUpdate.setTenantId(jsmileContext.getTenantId());
        reqSysResourceUpdate.setCreateBy(jsmileContext.getUserName());
        reqSysResourceUpdate.setUpdateBy(jsmileContext.getUserName());
        if (!sysResourcesApplicationService.saveResources(reqSysResourceUpdate)) {
            return JSmileResult.error("添加资源异常!");
        }
        return JSmileResult.success();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "修改资源")
    @JsmileLog(title = "用户中心->修改资源", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult updateDict(@RequestBody @Valid ReqSysResourceUpdate reqSysResourceUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysResourceUpdate.setTenantId(jsmileContext.getTenantId());
        reqSysResourceUpdate.setCreateBy(jsmileContext.getUserName());
        reqSysResourceUpdate.setUpdateBy(jsmileContext.getUserName());
        if (!sysResourcesApplicationService.updateResources(reqSysResourceUpdate)) {
            return JSmileResult.error("修改资源异常!");
        }
        return JSmileResult.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除资源")
    @JsmileLog(title = "用户中心->删除资源", type = LogTypeEnum.DELETE, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult deleteDict(@RequestBody @Valid ReqSysResourceUpdate reqSysResourceUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysResourceUpdate.setTenantId(jsmileContext.getTenantId());
        if (!sysResourcesApplicationService.deleteResources(reqSysResourceUpdate)) {
            return JSmileResult.error("删除资源异常!");
        }
        return JSmileResult.success();
    }
}
